<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>商品管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="./layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>

<body>

    <fieldset class="layui-elem-field" style="margin-top: 10px;">
        <div style="margin: 10px;">
            <a href="javascript:void(0)" style="height: 38px; line-height: 38px;"><strong>添加商品</strong></a>
            <a class="layui-btn" style="float: right;" href="./show.html">商品列表</a>
        </div>
    </fieldset>

    <form class="layui-form" action="">

        <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-inline">
                <input type="text" name="goods_name" lay-verify="required" autocomplete="off"
                    lay-reqtext="商品名称是必填项，岂能为空？" placeholder="请输入商品名称" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">商品类型</label>
            <div class="layui-input-inline">
                <input type="text" name="goods_type" autocomplete="off" placeholder="请输入商品货号"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">商品品牌</label>
            <div class="layui-input-block">
                <textarea name="goods_brand" placeholder="商品描述" class="layui-textarea" style="width: 35%"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">商品价格</label>
            <div class="layui-input-inline">
                <input type="text" name="goods_price" autocomplete="off" placeholder="请输入商品价格"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">商品分类</label>
            <div class="layui-input-inline">
                <select name="type_id" lay-filter="aihao" id="type_id">
                    <option value="0"> -- 请选择 -- </option>
                </select>
            </div>
        </div> 


         <div class="layui-upload">
          <button type="button" class="layui-btn" id="test1">上传图片</button>
          <div class="layui-upload-list">
            <img class="layui-upload-img" id="demo1" width="150px" height="150px">
            <input type="hidden" value="" name="img_one" id="img_one">
            <p id="demoText"></p>
          </div>
        </div>   
         
         
        <div class="layui-upload">
          <button type="button" class="layui-btn" id="test2">多图片上传</button> 
          <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
            预览图：
            <div class="layui-upload-list" id="demo2"></div>
             <input type="hidden" value="" name="img_more" id="img_more">
         </blockquote>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>

    <script src="./layui/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
    <script>
        layui.use(['form', 'jquery', 'upload'], function () {

            var form = layui.form
                , upload = layui.upload
                , $ = layui.$

                //普通图片上传
                  var uploadInst = upload.render({
                    elem: '#test1'
                    ,url: 'http://127.0.0.1/localhost/linaxi/php/public/index.php/index/Upload/upload'
                    ,before: function(obj){
                      //预读本地文件示例，不支持ie8
                      obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); //图片链接（base64）
                      });
                    }
                    ,done: function(res){
                      //如果上传失败
                      if(res.code > 0){
                        return layer.msg('上传失败');
                      }
                      //上传成功
                      $("#img_one").val(res.data.src)
                    }
                    ,error: function(){
                      //演示失败状态，并实现重传
                      var demoText = $('#demoText');
                      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                      demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                      });
                    }
                  });
                  
                  //多图片上传
                  upload.render({
                    elem: '#test2'
                    ,url: 'http://127.0.0.1/localhost/linaxi/php/public/index.php/index/Upload/upload'
                    ,multiple: true
                    ,before: function(obj){
                      //预读本地文件示例，不支持ie8
                      obj.preview(function(index, file, result){
                        $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                      });
                    }
                    ,done: function(res){
                      //上传完毕
                       // $("#img_more").val(res.data.src)

                        var src=$('#img_more').val();
                            src += res.data.src ;
                            src+=',';
                            $('#img_more').val(src);
                        }

                  });

           
            //监听提交
            form.on('submit(add)', function (data) {
                console.log( data.field )
                $.ajax({
                    method: 'post',
                    url: 'http://127.0.0.1/localhost/linaxi/php/public/index.php/index/admin/adds',
                    data: data.field,
                    dataType: 'json',
                    success: function (res) {
                        layer.alert( res.msg )
                        if ( res.code == 0 ) {
                            location.href = "./show.html"
                        }
                    }
                })
                return false;
            });

           

        });
    </script>

</body>

</html>